/**
 * 
 */
package com.iss.card.view.overrideview;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.LoginException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.iss.card.CardController;
import com.iss.card.CardController.EventType;
import com.iss.card.R;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.CommonValue;
import com.iss.card.common.FunctionUtil;
import com.iss.card.view.ContainerActivity;

/**
 * @author dtdh
 * 
 */
public class ToolBarView extends LinearLayout implements View.OnClickListener {
	private final static String TAG = ToolBarView.class.getSimpleName();
	private Context mContext;
	private List<MyImageSwitcher> mItems = new ArrayList<MyImageSwitcher>();

	/**
	 * @param context
	 */
	public ToolBarView(Context context) {
		super(context);
		// Log.d(TAG, "ToolBarView: c=" + context);
		mContext = context;
		initView(null);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public ToolBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// Log.d(TAG, "ToolBarView: c=" + context);
		mContext = context;
		initView(attrs);
	}

	@Override
	public void onClick(View v) {
		// Log.d(TAG, "onClick: v=" + v);
		ApplicationManager.IS_CARD_LIST_BY_STORE_ID = false;
		switch (v.getId()) {
		case R.id.store: // store list.
			FunctionUtil
					.setCurrentPageIndex(CommonValue.EnumPageIndex.EPageStoreList);
			CardController.getInstance(mContext).sendEvent(
					EventType.STORE_CONTENT);
			break;
		case R.id.card: // card list.

			if (FunctionUtil.isUserLogIn()) {
				FunctionUtil
						.setCurrentPageIndex(CommonValue.EnumPageIndex.EPageCardList);
				CardController.getInstance(mContext).sendEvent(
						EventType.CARDBAG_CONTENT);
			} else {
				Intent intentForLogin = new Intent(mContext,
						ContainerActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(ApplicationManager.KEY_LOGIN_FRAG_FIRED_BY,
						ApplicationManager.LOGIN_FROM_MYCARD);

				bundle.putInt(ApplicationManager.INTENT_KEY,
						ApplicationManager.INTENT_FRAMENT_LOG_IN);
				intentForLogin.putExtras(bundle);
				mContext.startActivity(intentForLogin);
			}
			break;
		case R.id.collect: // collected product list.
			FunctionUtil
					.setCurrentPageIndex(CommonValue.EnumPageIndex.EPageCollectList);
			CardController.getInstance(mContext).sendEvent(
					EventType.COLLENT_CONTENT);
			break;
		case R.id.myself: // setting.
			FunctionUtil
					.setCurrentPageIndex(CommonValue.EnumPageIndex.EPageSetting);
			CardController.getInstance(mContext).sendEvent(
					EventType.ACCOUNT_CONTENT);
			break;
		default:
			break;
		}
	}

	private void initView(AttributeSet attrs) {
		LayoutInflater layoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (layoutInflater == null) {
			throw new AssertionError("LayoutInflater not found.");
		}
		mItems.clear();
		LinearLayout v = (LinearLayout) layoutInflater.inflate(
				R.layout.item_button, null);
		ImageView store = (ImageView) v.findViewById(R.id.store);
		store.setOnClickListener(this);
		mItems.add(new MyImageSwitcher(store, R.drawable.home_btn,
				R.drawable.home_btn2));

		ImageView card = (ImageView) v.findViewById(R.id.card);
		card.setOnClickListener(this);
		mItems.add(new MyImageSwitcher(card, R.drawable.cardbag_btn,
				R.drawable.cardbag_btn2));

		ImageView collect = (ImageView) v.findViewById(R.id.collect);
		collect.setOnClickListener(this);
		mItems.add(new MyImageSwitcher(collect, R.drawable.collection_btn,
				R.drawable.collection_btn2));

		ImageView setting = (ImageView) v.findViewById(R.id.myself);
		setting.setOnClickListener(this);
		mItems.add(new MyImageSwitcher(setting, R.drawable.myself_btn,
				R.drawable.myself_btn2));

		// set tab icon as pressed according to current displayed view. that
		// means we always let displayed view's tab icon as pressed.
		mItems.get(getTagFromAttributeset(attrs)).setViewStatus(
				MyImageSwitcher.STATUS_PRESSED);
		v.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		addView(v);
	}

	private int getTagFromAttributeset(AttributeSet attrs) {
		if (attrs == null) {
			return CommonValue.EnumPageIndex.EPageStoreList;
		}

		final int num = attrs.getAttributeCount();
		for (int i = 0; i < num; ++i) {
			if ("tag".equals(attrs.getAttributeName(i))) {
				// Log.d(TAG, "getTagFromAttributeset: " +
				// attrs.getAttributeValue(i));
				return Integer.valueOf(attrs.getAttributeValue(i));
			}
		}
		return CommonValue.EnumPageIndex.EPageStoreList;
	}

	/**
	 * encapsulation class for view. it is used for those will switch background
	 * according to status.
	 * 
	 * @author iss
	 * 
	 */
	private final class MyImageSwitcher {
		/** view's default status */
		public final static int STATUS_NORMAL = 0x10;
		/** it means view have been clicked. */
		public final static int STATUS_PRESSED = 0x11;
		/** target one whose background will be switched. */
		public View mView;
		/** resource id for normal status. */
		public int mResIdNormal;
		/** resource id for pressed status. */
		public int mResIdPressed;

		public MyImageSwitcher(View v, int normalResId, int pressedResId) {
			mView = v;
			mResIdNormal = normalResId;
			mResIdPressed = pressedResId;
			setViewStatus(STATUS_NORMAL);
		}

		/**
		 * background will be switched according to status.
		 * 
		 * @param status
		 *            view's status, is it normal or pressed.
		 */
		public void setViewStatus(int status) {
			// Log.d(TAG, "setViewStatus: status=" + status + ", view=" +
			// mView);
			switch (status) {
			case STATUS_NORMAL:
				mView.setBackgroundResource(mResIdNormal);
				break;
			case STATUS_PRESSED:
				mView.setBackgroundResource(mResIdPressed);
				break;
			default:
				break;
			}
		}
	}

}
